package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private MinIoTemplate minIoTemplate;
    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public ResponseResult upload(MultipartFile multipartFile) throws IOException {
        //1.获取图片上传的输入流
        InputStream inputStream = multipartFile.getInputStream();
        //image/png
        //根据 / 分割
        //获取分割之后的后面那段内容
        String contentType = multipartFile.getContentType();
        if (StringUtils.isEmpty(contentType)){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        //{"image","png"}
        String[] split = contentType.split("/");
        //后缀名
        String suffixNmae = split[1];
        //2.图片上传到minio
        String url = minIoTemplate.uploadFile("", System.currentTimeMillis() + "." + suffixNmae, inputStream, "image");
        //3.将图片地址写入到数据库
        WmMaterial material = new WmMaterial();
        material.setUserId(UserThreadLocal.get().getId());
        material.setUrl(url);
        material.setIsCollection((short) 0);
        material.setType((short) 0);
        material.setCreatedTime(new Date());
        int insertResult = wmMaterialMapper.insert(material);
        if (insertResult < 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        //4 将信息返回前端
        return ResponseResult.okResult(material);
    }

    @Override
    public PageResponseResult list(WmMaterialDto dto) {
        //入参判空
        if (dto == null){
            return new PageResponseResult(0,0,0);
        }
        //1.构造查询条件
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        //1.1 当isCondition为1时才能让条件生效
        wrapper.eq(dto.getIsCollection() == 1,WmMaterial::getIsCollection,dto.getIsCollection());
        //2.构建分页条件
        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
        //3.执行分页查询
        Page<WmMaterial> pageResult = wmMaterialMapper.selectPage(page, wrapper);
        //4.构建返回值结构
        Long total = pageResult.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(),dto.getSize(),total.intValue());
        pageResponseResult.setData(pageResult.getRecords());

        return pageResponseResult;
    }
}
